package com.ygxsk.carhome.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.Utils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.alertview.AlertView;
import com.ygxsk.carhome.alertview.OnItemClickListener;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.listener.DoubleClickListener;
import com.ygxsk.carhome.utils.CommUtils;
import com.ygxsk.carhome.utils.ViewUitls;


//interface OnRequestListenFragment {
//
//    void onClickNodata(View view);
//
//}

//@AndroidEntryPoint
public abstract class FragmentClamour extends FragmentEventSK implements OnRequestListenFragment {

    // __________________________________________________________________
    /**
     * 总体的外侧内容
     */
    protected FrameLayout loading;
    protected TextView no_data_one;

    private RelativeLayout layoutNoData;
    private RelativeLayout layoutNullData;
    private ImageView imageViewErrorNull;
    private TextView textViewErrorNull;

    // __________________________________________________________________

    //protected boolean isVisible;

    //如下是两个比较重要的引用_________________________________________________________________
    //可以去掉this引用
    protected FragmentClamour mFragmentInstance;

    /**
     * 做父窗体的引用
     */
    protected Activity mActivityInstance;
    //__________________________________________________________________

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mFragmentInstance = this;
        mActivityInstance = this.getActivity();

        //_sessionErrorActivity = Session.get(this.getActivity());

    }


//    @Override
//    public void onConfigurationChanged(@NonNull Configuration newConfig) {
//        if (newConfig.fontScale != 1) {//非默认值
//            getResources();
//        }
//        super.onConfigurationChanged(newConfig);
//    }

    protected abstract void gainDatas();

    /**
     * 做缓存的数据的空实现
     */
    protected void methodLoadCache(String jsonStringCache) {
        //
    }

    /**
     * 做非20000的界面处理的
     */
    public void methodNotSuccessData(int what_method, String serverCode, String serverMessage) {

    }

    protected void initLoading(View view) {

        if (view != null) {

            loading = (FrameLayout) view.findViewById(R.id.loadingfragmentall);

            if (loading != null) {

                //只有当网络不给力的时候这个屏幕是可以点击的
                if (NetworkUtils.isConnected()) {//网络可用 这个布局是不可点击的

                    loading.setFocusable(false);
                    loading.setPressed(false);

                    loading.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // Consume click
                        }
                    });

                } else {//网络不可用 那么 这个就是重试


                    loading.setFocusable(true);
                    loading.setPressed(true);//网络未连接具备点击事件。

                    loading.setOnClickListener(new NodataClicker());//界面上刷新重试

				/*loading.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// Consume click
					}
				});*/

                }

                layoutNoData = ViewUitls.getLayoutNoData(loading);
                no_data_one = ViewUitls.getNo_data_one(loading);

                //buttonTryAgain = ViewUitls.getButtonTryAgain(loading);
                //buttonTryAgain.setOnClickListener(new NodataClicker());

                //================================================================
                layoutNullData = ViewUitls.getLayoutNullData(loading);
                imageViewErrorNull = ViewUitls.getImageViewErrorNull(loading);
                textViewErrorNull = ViewUitls.getTextViewErrorNull(loading);

                //再次做数据隐藏
                if (loading.getVisibility() == View.VISIBLE) {
                    loading.setVisibility(View.GONE);
                }
            }
        }
    }

    public class NodataClicker implements OnClickListener {

        private AlertView alertViewNet;

        @Override
        public void onClick(View v) {

            if (!DoubleClickListener.isFastDoubleClick()) {

                if (NetworkUtils.isConnected()) {

                    onClickNodata(v);

                } else {

                    CommUtils.checkDialog(alertViewNet);

                    //打开网络设置
                    alertViewNet = new AlertView(ConstantSign.ALERT_TITLE, //
                            ConstantSign.NET_WORKSETTING, //
                            getString(R.string.alert_cancel), //
                            new String[]{getString(R.string.alert_open)}, //
                            null, //
                            FragmentClamour.this.getActivity(),//
                            AlertView.Style.Alert, //
                            new OnItemClickListener() {//
                                @Override
                                public void onItemClick(Object o, int position) {//
                                    if (position != AlertView.CANCELPOSITION) {
                                        NetworkUtils.openWirelessSettings();//
                                    }
                                }//
                            });

                    alertViewNet.setCancelable(true)//
                            .show();//
                }
            }

        }

    }


    @Override
    public void onDestroy() {

        //取消网络的请求和生命周期开始绑定--2018年01月15日先这样，后期在观察是否改进。
        //和声明周期绑定，退出时取消这个队列中的所有请求，当然可以在你想取消的时候取消也可以，不一定和声明周期绑定。
        //CallServer.getInstance().cancelBySign(object);.//这个会引起一个问题，反复切换导致数据不加载
        super.onDestroy();

        //leakcrash
        /*RefWatcher refWatcher = DingGovernApplication.getRefWatcher(getActivity());
        if (refWatcher!=null){
            refWatcher.watch(this);
        }*/

        if (onEventlistener != null) {
            onEventlistener = null;
        }

    }

    protected void sendMsg(Handler mBaseHandler, int what, Object obj) {
        Message msg = mBaseHandler.obtainMessage(what);
        msg.obj = obj;
        mBaseHandler.sendMessage(msg);
    }

    public void loadingGone() {

        if (loading != null) {

            ViewUitls.LoadingGone(loading);

            if (layoutNoData != null) {
                if (layoutNoData.getVisibility() == View.VISIBLE) {
                    layoutNoData.setVisibility(View.GONE);
                }
            }

            if (layoutNullData != null) {
                if (layoutNullData.getVisibility() == View.VISIBLE) {
                    layoutNullData.setVisibility(View.GONE);
                }
            }

        }

    }

    public void fillNullDataView(String message, int imageResourecesId) {

        if (loading != null) {

            //LogUtils.d("itchen---fillNullDataView--->loading 不是null");

            ViewUitls.LoadingVisible(loading);

            if (layoutNoData != null) {
                if (layoutNoData.getVisibility() == View.VISIBLE) {
                    layoutNoData.setVisibility(View.GONE);
                }
            }

            if (layoutNullData != null) {
                if (layoutNullData.getVisibility() == View.GONE) {
                    layoutNullData.setVisibility(View.VISIBLE);
                }
            }

            if (textViewErrorNull != null) {
                textViewErrorNull.setText(message);
            }

            if (imageViewErrorNull != null) {

                //2018年02月06日封装为动态填充空界面图片。
                imageViewErrorNull.setImageDrawable(ContextCompat.getDrawable(Utils.getApp(), imageResourecesId));

            }

        } else {
//            LogUtils.d("itchen---fillNullDataView--->loading 是 null");
        }

    }


    public void netWorkError() {

        if (isAdded()) {

            if (loading != null) {

                ViewUitls.LoadingVisible(loading);
                //=============================================================
                if (layoutNoData != null) {
                    if (layoutNoData.getVisibility() == View.GONE) {
                        layoutNoData.setVisibility(View.VISIBLE);
                    }
                }
                //=============================================================
                if (layoutNullData != null) {
                    if (layoutNullData.getVisibility() == View.VISIBLE) {
                        layoutNullData.setVisibility(View.GONE);
                    }
                }
                //=============================================================
            }

        }

    }

    /**
     * 关闭上拉加载更多效果
     */
    public void setEnableLoadmore(SmartRefreshLayout smartRefreshLayout, boolean isEnable) {
        //这里不让上拉了
        if (smartRefreshLayout != null) {
            smartRefreshLayout.setEnableLoadMore(isEnable);
        }
    }

}
